﻿using System;
using System.Windows;
using System.Windows.Controls;

namespace Notoric.Gui.Windows
{
    /// <summary>
    /// Interaction logic for ChangeTimeSignatureDialog.xaml
    /// </summary>
    public partial class ChangeTimeSignatureDialog : Window
    {
        #region Constructors

        /// <summary>
        /// Initializes a new instance of the <see cref="ChangeTimeSignatureDialog"/> class.
        /// </summary>
        public ChangeTimeSignatureDialog()
        {
            InitializeComponent();
        }

        #endregion

        #region Event Handlers

        /// <summary>
        /// Handles the Click event of the PART_OK control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="System.Windows.RoutedEventArgs"/> instance containing the event data.</param>
        private void PART_OK_Click(object sender, RoutedEventArgs e)
        {
            var composition = (ViewModel.Elements.Composition)DataContext;
            ViewModel.Elements.TimeSignatureType signatureType = ViewModel.Elements.TimeSignatureType.Standard;
            ushort beats = 4, beatUnit = 4;

            if (PART_CommonType.IsChecked == true)
                signatureType = ViewModel.Elements.TimeSignatureType.Common;
            else if (PART_AllaBreveType.IsChecked == true)
                signatureType = ViewModel.Elements.TimeSignatureType.AllaBreve;
            else if (PART_44.IsChecked == true)
            {
                beats = 4;
                beatUnit = 4;
            }
            else if (PART_34.IsChecked == true)
            {
                beats = 3;
                beatUnit = 4;
            }
            else if (PART_22.IsChecked == true)
            {
                beats = 2;
                beatUnit = 2;
            }
            else if (PART_68.IsChecked == true)
            {
                beats = 6;
                beatUnit = 8;
            }
            else if (PART_98.IsChecked == true)
            {
                beats = 9;
                beatUnit = 8;
            }
            else
            {
                beats = (ushort)PART_Beats.Value;
                beatUnit = ushort.Parse(((ComboBoxItem)PART_BeatUnit.SelectedItem).Content.ToString());
            }

            var fromMeasure = (ushort)Math.Max(PART_FromMeasure.Minimum, Math.Min(PART_FromMeasure.Value, PART_FromMeasure.Maximum));
            var toMeasure = (ushort)Math.Max(PART_ToMeasure.Minimum, Math.Min(PART_ToMeasure.Value, PART_ToMeasure.Maximum));

            var cmd = new ViewModel.ChangeTimeSignatureCommand(composition.UndoStack, composition.RedoStack)
            {
                Beats = beats,
                BeatUnit = beatUnit,
                Composition = composition,
                FromMeasure = fromMeasure,
                ToMeasure = toMeasure,
                Type = signatureType
            };

            cmd.Execute(null);

            this.DialogResult = true;
        }

        /// <summary>
        /// Handles the Click event of the PART_Cancel control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="System.Windows.RoutedEventArgs"/> instance containing the event data.</param>
        private void PART_Cancel_Click(object sender, RoutedEventArgs e)
        {
            this.DialogResult = false;
        }

        #endregion
    }
}
